package com.dapeis.web.admin.controller;

import java.util.Date;
import java.util.Iterator;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import com.ag.core.base.Pager;
import com.ag.core.exception.SysTipsException;
import com.ag.core.utils.AgAssert;
import com.ag.dao.PagerHelper;
import com.ag.module.admin.domain.AdminUser;
import com.dapeis.core.utils.DBConfigUtils;
import com.dapeis.core.utils.MessagePushConfig;
import com.dapeis.core.utils.StylistConstants;
import com.dapeis.domain.Collocation;
import com.dapeis.domain.MoneyLog;
import com.dapeis.domain.User;
import com.dapeis.domain.ext.CollocationExt;
import com.dapeis.domain.ext.MoneyLogExt;
import com.dapeis.domain.ext.TaskOrderExt;
import com.dapeis.service.CollocationService;
import com.dapeis.service.JPushService;
import com.dapeis.service.MoneyLogService;
import com.dapeis.service.TIMService;
import com.dapeis.tx.push.MoneyOutLayOKNoticeText;

/**
 * @author 作者 E-mail: hzyuanyong@126.com
 * @version 创建时间：2017年5月24日 下午10:03:17 类说明
 */
@Controller
@RequestMapping("/admin/moneyLog")
public class AdminMoneyLogController extends AdminController {

    @Resource
    MoneyLogService moneyLogService;
    @Resource
    JPushService jPushService;
    @Resource
    CollocationService collocationService;

    @RequestMapping("/list")
    public String list(
	    @RequestParam(value = "pager", required = false) Pager pager,
	    Map<Object, Object> model, RedirectAttributes attr) {
	if (null == pager) {
	    pager = new Pager();
	}
	Example example = new Example(MoneyLog.class);
	Criteria criteria = example.createCriteria();
	criteria.andEqualTo(MoneyLog.PROPERTY_TYPE,
		StylistConstants.MoneyLog.Type.OUTLAY);
	example.orderBy(MoneyLog.PROPERTY_CREATETIME).desc();
	pager.setExample(example);
	moneyLogService.pager(pager);
	if (!(pager.getItemList().isEmpty())) {
	    PagerHelper.pager(pager, moneyLogService.getMoneyLogExtMapper());
	    doInitCollocationData(pager);
	}
	model.put("pager", pager);
	return doPackageURI("moneyLog/list");

    }

    @RequestMapping("/{moneyLogId}/detail")
    public String detial(@PathVariable Integer moneyLogId,
	    Map<Object, Object> model, RedirectAttributes attr) {
	MoneyLog moneyLog = moneyLogService.selectByPrimaryKey(moneyLogId);
	AgAssert.notNull(moneyLog, SysTipsException.class, "查询不到提现 ID："
		+ moneyLogId);
	model.put("moneyLog", moneyLog);
	return doPackageURI("moneyLog/detail");
    }

    @RequestMapping("/{moneyLogId}/doAudit")
    public String doAudit(@PathVariable Integer moneyLogId, String resutCode,
	    String message, HttpSession session, RedirectAttributes attr) {
	MoneyLog moneyLog = moneyLogService.selectByPrimaryKey(moneyLogId);
	AgAssert.notNull(moneyLog, SysTipsException.class, "查询不到提现 ID："
		+ moneyLogId);
	AgAssert.isTrue(
		moneyLog.getStatus() == StylistConstants.MoneyLog.Status.OUTLAY_COMMIT,
		SysTipsException.class, "该提现记录已处理!");
	AdminUser adminUser = getSessionAdminUser(session);
	moneyLog.setAdminUserId(adminUser.getId());
	moneyLog.setMessage(message);
	moneyLog.setAuditTime(new Date().getTime() / 1000);
	if (resutCode.equalsIgnoreCase("pass")) {
	    moneyLog.setStatus(StylistConstants.MoneyLog.Status.OUTLAY_AUDIT_SUCCESS);
	} else if (resutCode.equalsIgnoreCase("fail")) {
	    moneyLog.setStatus(StylistConstants.MoneyLog.Status.OUTLAY_AUDIT_FAIL);
	}
	moneyLogService.updateByPrimaryKeySelective(moneyLog);
	moneyLogService.doAccountMoney(null, moneyLog);
	setInfoMsg("处理成功!", attr);
	if (resutCode.equalsIgnoreCase("pass")) {
	    // 转账成功,提示消息
	    jPushService.pushMessage(moneyLog.getCollocationId(),
		    new MoneyOutLayOKNoticeText());
	}

	return redirect("/admin/moneyLog/list");
    }

    void doInitCollocationData(Pager pager) {
	if (!(pager.getItemList().isEmpty())) {
	    for (Iterator iter = pager.getItemList().iterator(); iter.hasNext();) {
		MoneyLogExt mle = (MoneyLogExt) iter.next();
		if (null != mle && null != mle.getCollocationId()) {
		    mle.setCollocationExt(collocationService
			    .getCollocationExtMapper().selectByPrimaryKey(
				    mle.getCollocationId()));
		}
	    }
	}
    }
}
